#include <memory>
#include "reactor.hpp"
#include "listener.hpp"
#include "package_parse.hpp"

bool useTips(int argc)
{
    if(argc != 2)
    {
        std::cout << "poll_server fail to use, the right way to use:poll_server [port]\n";
        return false;
    }
    return true;
}

int main(int argc, char* argv[])
{
    if(!useTips(argc))
        return -1;

    HanderConnection hc(PackageParse::prase); // io通信+上层业务回调

    Listener listener(stoi(argv[1]), hc); //定义监听模块

    std::unique_ptr<Reactor> rct = std::make_unique<Reactor>(); // 定义事件派发器

    // 将listen套接字加入到reactor中
    rct->addConnection(listener.getListenFd(), 
        EPOLLIN | EPOLLET, 
        std::bind(&Listener::acceptFd, &listener, std::placeholders::_1), nullptr, nullptr);

    // 启动派发器
    rct->dispatch();

    return 0;
}